<template>
   <a-spin tip="数据导入中..." :spinning="spinning">
   <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="干部编号">
                <a-input v-model="queryParam.code" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="干部姓名">
                <a-input v-model="queryParam.name" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'left', overflow: 'hidden' }">
                <a-button type="primary" @click="handleQuery">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
         
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">添加干部信息</a-button>
		<a-divider type="vertical" />
		<a-button type="primary" icon="download" @click="downloadFile">下载模板</a-button>
		<a-divider type="vertical" />
		<a-button type="primary" icon="export" @click="exportFile">导出文本数据</a-button>
		<a-divider type="vertical" />
		<a-upload :customRequest="importData" :fileList="[]">
			<a-button type="primary" icon="import">导入数据</a-button>
		</a-upload>
      </div>
      <s-table
          ref="table"
          size="default"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          showPagination="auto"
          :scroll="{ x: 2280 }"
          bordered
        >
          <span slot="action" slot-scope="text, record">
             <a @click="handleEdit(record)"> <a-tag color="blue">编辑</a-tag></a>
			 <a-divider type="vertical" />
			 <a-popconfirm
			   title="是否删除该条记录?"
			   @confirm="handleDel(record)"
			   @cancel="cancel"
			   okText="是"
			   cancelText="否"
			 >
			   <a href="#"><a-tag color="blue">删除</a-tag></a>
			 </a-popconfirm>
			 
            </span>
        </s-table>
       <leader-info-from
         ref="laderinfo"
         :title="title"
         :visible="visible"
         :model="mdl"
         @cancel="handleCancel"
         @ok="handleOk"
       />
    </a-card>
	</a-spin>
</template>

<script>
  import { STable } from '@/components'
  import {getUrl,postAxiosUrl,uploadUrl} from '@/api/manage'
  import my from '../../api/my/my'
  import LeaderInfoFrom from './modules/leaderinfoModal'
  export default {
    components: {
      STable,
	  LeaderInfoFrom
    },
    data(){
      return {
         title:'新建',
         visible: false,
         confirmLoading: false,
         mdl: null,
		 spinning : false,
         queryParam: {},
         loadData: parameter => {
           const requestParameters = Object.assign({}, parameter, this.queryParam)
           return getUrl('leaderInfo/list', requestParameters).then(res => {
             return res.result
           })
         },
         selectedRowKeys: [],
         selectedRows: [],
      }
    },
    computed: {
      columns() {
        return  [
          {title :'编号',dataIndex: 'code',fixed: 'left',width: '90px'},
          {title :'姓名',dataIndex: 'name',fixed: 'left',width: '90px'},
          {title: '年龄',dataIndex: 'age',width: '90px'},
          {title: '性别',dataIndex: 'sex',width: '90px'},
          {title: '民族',dataIndex: 'nation',width: '90px'},
          {title: '部门',dataIndex: 'departmentName',width: '140px'},
          {title: '现任职务',dataIndex: 'position',width: '100px'},
          {title: '政治面貌',dataIndex: 'politicsStatus',width: '100px'},
          {title :'状态',dataIndex: 'status',width: '110px'},
          {title :'健康状况',dataIndex: 'healthCondition',width: '100px'},
          {title: '生日',dataIndex: 'birthday',width: '120px'},
          {title: '学历',dataIndex: 'major',width: '120px'},
          {title: '专业',dataIndex: 'education',width:'200px',},
          {title: '学位',dataIndex: 'degree',width: '90px'},
          {title: '任现职年限',dataIndex: 'rxznx',width: '120px'},
          {title: '任现职级时间',dataIndex: 'currentPositionGradeDate',width: '120px'},
          {title: '任现职级年限',dataIndex: 'rxzjnx',width: '120px'},
          {title: '任正副科时间',dataIndex: 'rzfksj',width: '120px'},
          {title: '任正副科年限',dataIndex: 'rzfknx',width: '120px'},
          {title: '操作',dataIndex: 'action',fixed: 'right',scopedSlots: { customRender: 'action' }},
        ]
      }
    },
	created() {
		getUrl('cutDictionaryDetail/getAllData', {}).then(res => {
			this.$refs.laderinfo.positionData = res.result["003"]
			this.$refs.laderinfo.positionGradeData = res.result["004"]
			this.$refs.laderinfo.politicsStatusData = res.result["014"]
			this.$refs.laderinfo.gradeData = res.result["002"]
			this.$refs.laderinfo.trainingTypeData = res.result["010"]
			this.$refs.laderinfo.trainingOrientationData = res.result["011"]
			this.$refs.laderinfo.statusData = res.result["012"]
			this.$refs.laderinfo.youngLeaderTypeData = res.result["013"]
			this.$refs.laderinfo.healthConditionData = res.result["015"]
			this.$refs.laderinfo.leaderTypeData = res.result["018"]
			this.$refs.laderinfo.majorData = res.result["019"]
			this.$refs.laderinfo.degreeData = res.result["020"]
			this.$refs.laderinfo.educationData = res.result["021"]
			this.$refs.laderinfo.nationData = res.result["022"]
			this.$refs.laderinfo.assessTypeData = res.result["023"]
			this.$refs.laderinfo.educationTypeData = res.result["024"]
			this.$refs.laderinfo.educationCategoryData = res.result["025"]
		})
	},
    methods: {
	  getPageData(record,params={}) {
	      getUrl('department/getSelectTrees', Object.assign(params, this.queryParam)).then(data => {
	          this.$refs.laderinfo.dpttreeData = data.result
	      })
		  getUrl('subordinateUnit/list', Object.assign(params, {leaderInfoId : (record ? record.id : '')})).then(data => {
		      this.$refs.laderinfo.form.workdata = data.result
		  })
		  getUrl('rewards/list', Object.assign(params, {leaderInfoId : (record ? record.id : '')})).then(data => {
		      this.$refs.laderinfo.form.rewardsdata = data.result
		  })
		  getUrl('family/list', Object.assign(params, {leaderInfoId : (record ? record.id : '')})).then(data => {
		      this.$refs.laderinfo.form.familydata = data.result
		  })
		  getUrl('assess/list', Object.assign(params, {leaderInfoId : (record ? record.id : '')})).then(data => {
		      this.$refs.laderinfo.form.assessdata = data.result
		  })
	  },
      handleQuery() {
        this.$refs.table.refresh(true)
      },
      handleReset(){
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      handleEdit(record) {
        this.clearFields();
        this.mdl =  Object.assign(this.mdl, {...record})
		this.getPageData(record)
        this.title =  '编辑'
        this.visible = true
      },
	  cancel(e) {
	  },
	  handleDel(record) {
		  postAxiosUrl('leaderInfo/delete',{id:record.id}).then(res=>{
		     this.$refs.table.refresh()
		  })
	  },
      clearFields () {
        this.mdl = {
			id:'',
			name: '',
			code:'',
			age: '',
			sex: undefined,
			nation: undefined,
			birthday : '',
			major : undefined,
			education : undefined,
			degree : undefined,
			academy: '',
			nativePlace:'',
			politicsStatus:undefined,
			birthplace:'',
			joinPartyDate:'',
			workDate:'',
			grade:undefined,
			position:undefined,
			positionType:undefined,
			takePosition:undefined,
			removePosition:undefined,
			currentPositionGradeDate:undefined,
			trainingOrientation:undefined,
			trainingType:undefined,
			department:undefined,
			professionalExpertise:undefined,
			professionalTitle:undefined,
			healthCondition:undefined,
			youngLeaderDate:undefined,
			youngLeaderType:undefined,
			isleader:undefined,
			status:undefined,
			leaderType:undefined,
			onJobEducation:undefined,
			onJobMajor:undefined,
			onJobDegree:undefined,
			onJobAcademy:undefined,
			assessDate:undefined,
			assessDate:undefined,
			assessDetail:undefined,
			educationType:undefined,
			educationCategory:undefined,
			onJobEducationCategory:undefined,
      idcode:undefined,
      mobile:undefined,
      xg:undefined,
      ah:undefined,
      bz:undefined,
      gzqk:undefined,
      address:undefined,
      housearea:undefined,
      isjjst:undefined,
      jjstname :undefined,
      jydd:undefined,
      jtnsr:undefined,
      rxznx:undefined,
      rxzjnx:undefined,
      rzfksj:undefined,
      rzfknx:undefined,
      rxzsj:undefined,
			imageUrl: '',
			workdata:[],
			familydata:[],
			rewardsdata:[],
			assessdata:[]
        }
      },
      handleAdd () {
		this.getPageData(null)
        this.clearFields();
        this.title =  '新建'
        this.visible = true
      },
      handleOk (li) {
         li.$refs.liForm.validate(valid => {
          if(!valid){
             this.confirmLoading = true
			 this.$refs.laderinfo.loading = false
          } else {
            li.$refs.liForm.clearValidate()
            postAxiosUrl('leaderInfo/save',{...li.form}).then(res=>{
               this.confirmLoading = false
               this.visible = false
			   this.$refs.laderinfo.loading = false
               this.$refs.table.refresh()
            })
          }
        })
      },
      handleCancel (li) {
        this.visible = false
        li.$refs.liForm.clearValidate()
      },
      onClose() {
        this.visible = false;
        this.$refs.laderinfo.$refs.liForm.clearValidate()
      },
	  downloadFile(){
		  const a = document.createElement('a')
		  a.href = my.baseURL + `leaderInfo/downloadfile`
		  a.click()
	  },
		exportFile() {
			const a = document.createElement('a')
			a.href = my.baseURL + `pulldatas/txtfile`
			a.click()
		},
	  // 重写a-upload的文件上传处理方式
	  importData (data) {  
	      this.spinning = true
	      this.saveFile(data)  
	  },  
	  // 上传并保存文件
	  saveFile (data){  
	    var _this = this
	    const formData = new FormData()  
	    formData.append('file', data.file)  
	    uploadUrl('leaderInfo/uploadfile',formData).then((res) => {
	      console.log(res)
	      if (res.data.success){
	        _this.spinning = false
	         _this.$message.info(res.data.result)
	         _this.queryParam = {}
	        this.$refs.table.refresh(true)
	      }else{
	        _this.spinning = false
	        _this.$message.error(res.data.error)
	      }
	    })
	  },
    }
  }
</script>

<style>
	.ant-select-dropdown-menu-item-selected,
	.ant-select-dropdown-menu-item-selected:hover {
	  color: #52c41a;
	  font-weight: 700;
	  background-color: #fafafa;
	}
</style>
